﻿using FastCloud.Base;
using FastCloud.Extend.DB;
using FastCloud.Extend.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Extend
{
    [Route("frame/extend/task")]
    public class TaskController : BaseController
    {
        [AutoWired]
        private DB_Task DB_Task;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Extend/Task/Task_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/Extend/Task/Task_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Extend/Task/Task_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string TaskName = ValueConvert.ConvertToString(GetFormParam("TaskName"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(TaskName))
            {
                sql += " and TaskName like @TaskName";
                ParamList.Add(new ParamEntity() { ParamName = "TaskName", ParamValue = "%" + TaskName + "%" });
            }

            return Result.OK(DB_Task.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Task Entity)
        {
            DB_Task.Insert(Entity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Task Entity)
        {
            DB_Task.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("updateTaskStatus"), HttpPost]
        public IActionResult UpdateTaskStatus()
        {
            string TaskGuid = ValueConvert.ConvertToString(GetFormParam("TaskGuid"));
            int TaskStatus = ValueConvert.ConvertToInt32(GetFormParam("TaskStatus"));
            Frame_Task Entity = DB_Task.SelectOne(TaskGuid);
            Entity.TaskStatus = TaskStatus;
            DB_Task.UpdateByID(Entity);
            return Result.OK();
        }


        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string TaskGuid = ValueConvert.ConvertToString(GetFormParam("TaskGuid"));
            return Result.OK(DB_Task.SelectOne(TaskGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            DB_Task.DeleteBatch(KeyList);
            return Result.OK();
        }
    }
}
